package controller

import (
	"github.com/mitchellh/mapstructure"
	"log"
	"mssgserver/constant"
	"mssgserver/net"
	"mssgserver/server/common"
	"mssgserver/server/game/logic"
	"mssgserver/server/game/model"
	"mssgserver/server/game/model/data"
	"mssgserver/utils"
	"time"
)

var DefaultRoleController = &RoleController{}

type RoleController struct {
}

func (r *RoleController) Router(router *net.Router) {
	group := router.Group("role")
	group.AddRouter("enterServer", r.enterServer)
	group.AddRouter("myProperty", r.myProperty)
	group.AddRouter("posTagList", r.posTagList)
}

/*
*
进入游戏的逻辑
*/
func (r *RoleController) enterServer(req *net.WsMsgReq, rsp *net.WsMsgRsp) {
	//进入的游戏的逻辑
	//Session 需要验证是否合法 合法的情况下 可以取出登录的用户id
	//根据用户id 去查询对应的游戏角色，如果有 就继续 没有 提示无角色
	//根据角色id 查询角色拥有的资源 roleRes，如果资源有 返回，没有 初始化资源
	//log.Println(gameConfig.Base)
	reqObj := &model.EnterServerReq{}
	rspObj := &model.EnterServerRsp{}
	//校验参数是否合法
	rsp.Body.Seq = req.Body.Seq
	rsp.Body.Name = req.Body.Name
	err := mapstructure.Decode(req.Body.Msg, reqObj)
	if err != nil {
		log.Println("enterServer参数转换失败")
		rsp.Body.Code = constant.InvalidParam
		return
	}
	//校验token
	session := reqObj.Session
	_, claims, err := utils.ParseToken(session)
	if err != nil {
		log.Println("enterServer解析token失败")
		rsp.Body.Code = constant.SessionInvalid
		return
	}
	uid := claims.Uid
	//查询role和roleRes
	err = logic.RoleService.EnterServer(uid, rspObj, req)
	if err != nil {
		rspObj.Time = time.Now().UnixNano() / 1e6
		rsp.Body.Msg = rspObj
		rsp.Body.Code = err.(*common.MyError).Code()
		return
	}
	rsp.Body.Code = constant.OK
	rsp.Body.Msg = rspObj
}

/*
*
获取角色的属性
*/
func (r *RoleController) myProperty(req *net.WsMsgReq, rsp *net.WsMsgRsp) {
	//分别根据角色id 去查询 军队 资源 建筑 城池 武将
	role, err := req.Conn.GetProperty("role")
	if err != nil {
		log.Println("role.myProperty获取role失败")
		rsp.Body.Code = constant.SessionInvalid
		return
	}
	rsp.Body.Seq = req.Body.Seq
	rsp.Body.Name = req.Body.Name
	rspObj := &model.MyRolePropertyRsp{}
	rid := role.(*data.Role).RId //拿到rid
	//获取城池
	roleCitys, err := logic.RoleCityService.GetCitys(rid)
	if err != nil {
		rsp.Body.Code = err.(*common.MyError).Code()
		return
	}
	rspObj.Citys = roleCitys
	//获取建筑
	roleBuilds, err := logic.RoleBuildService.GetBuilds(rid)
	if err != nil {
		rsp.Body.Code = err.(*common.MyError).Code()
		return
	}
	rspObj.MRBuilds = roleBuilds
	//获取资源
	roleRes, err := logic.RoleService.GetRoleRes(rid)
	if err != nil {
		rsp.Body.Code = err.(*common.MyError).Code()
		return
	}
	rspObj.RoleRes = roleRes
	//获取武将
	generals, err := logic.GeneralService.GetGenerals(rid)
	if err != nil {
		rsp.Body.Code = err.(*common.MyError).Code()
		return
	}
	rspObj.Generals = generals
	//获取军队
	armys, err := logic.ArmyService.GetArmys(rid)
	if err != nil {
		rsp.Body.Code = err.(*common.MyError).Code()
		return
	}
	rspObj.Armys = armys

	rsp.Body.Code = constant.OK
	rsp.Body.Msg = rspObj
}

/*
*
标记列表
*/
func (r *RoleController) posTagList(req *net.WsMsgReq, rsp *net.WsMsgRsp) {
	listRsp := model.PosTagListRsp{}
	rsp.Body.Seq = req.Body.Seq
	rsp.Body.Name = req.Body.Name
	role, err := req.Conn.GetProperty("role")
	if err != nil {
		log.Println("conn get role err", err)
		rsp.Body.Msg = err.Error()
		rsp.Body.Code = constant.SessionInvalid
		return
	}
	rid := role.(*data.Role).RId
	tags, err := logic.RoleAttributeService.GetPosTags(rid)
	if err != nil {
		rsp.Body.Code = err.(*common.MyError).Code()
		return
	}
	listRsp.PosTags = tags
	rsp.Body.Code = constant.OK
	rsp.Body.Msg = listRsp
}
